//
//  MoreController.m
//  万物记
//
//  Created by Ajen on 2019/6/16.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "MoreController.h"
#import "SetHomeTabController.h"
#import "SetLocalWordsController.h"
#import "RecoverDataController.h"

#import "DYAdAlertView.h"
#import "DYAdModel.h"

#import "PrivacyController.h"
#import "SuggestController.h"
#import "AboutController.h"

@interface MoreController ()<UITableViewDelegate,UITableViewDataSource,DYAdAlertDelegate>{
    NSArray<NSArray*> * kTitleArray;
}
@property(nonatomic,strong)UITableView * tableView;
@end

@implementation MoreController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithAjenHex:0x7a7374 alpha:1];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    kTitleArray = @[@[@"首页栏目管理",@"快捷词条管理",@"导出/备份数据",@"导入/恢复数据",@"使用教程"],@[@"隐私政策",@"意见或建议",@"关于"]];
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.aj_Text(@"万物记").aj_textColorHexStr(@"#2d2e36").aj_addToView(self.view);
    titleLabel.font = SongKeFont(30);
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@44);
    }];
    self.navigationItem.titleView = titleLabel;
    
    
    UILabel * topLabel = [UILabel new];
    topLabel.aj_Text(@"记住你的Anything~").aj_textColorHexStr(@"#ffffff").aj_addToView(self.view);
    topLabel.font = SongKeFont(19);
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel * bottomLabel = [UILabel new];
    bottomLabel.aj_Text(@"万物记").aj_textColor([UIColor colorWithAjenHex:0xffffff alpha:0.3]).aj_addToView(self.view);
    bottomLabel.font = SongKeFont(60);
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-40);
        make.centerX.equalTo(self.view);
    }];
   
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return kTitleArray[section].count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return kTitleArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier =@"UITableViewCell";
    UITableViewCell *cell = [self.tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UIAccessibilityTraitNone;
    }
    cell.textLabel.text = kTitleArray[indexPath.section][indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithAjenHex:0x2d2e36 alpha:0.8];
    cell.textLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightThin];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            SetHomeTabController * setHomeTabsController = [SetHomeTabController new];
            [self.navigationController pushViewController:setHomeTabsController animated:YES];
        }
        if(indexPath.row == 1){
            SetLocalWordsController * setWordsController = [SetLocalWordsController new];
            [self.navigationController pushViewController:setWordsController animated:YES];
        }
        if(indexPath.row == 2){
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击\"备份\"按钮,当前本地储存数据将会添加至手机剪贴板,将会覆盖之前的[复制]的内容,是否继续?\n若点击\"备份\"按钮,复制的内容可在本机或者另一台设备通过\"导入/恢复\"功能实现数据的转移和同步" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"备份" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                [pasteboard setString:[LocalModel shared].model.mj_JSONString];
                ShowTextHUD(@"以备份到剪贴板");
            }];
            [alert addAction:cameraAction];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
           
        }
        if(indexPath.row == 3){
            RecoverDataController * recoverDataController = [RecoverDataController new];
            [self.navigationController pushViewController:recoverDataController animated:YES];
        }
        if(indexPath.row == 4){
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"查看[首页]使用教程" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSArray * imgNameArr = @[@"首页插入新栏目.png",@"栏目重命名.png",@"首页新增内容.png",@"首页插入内容.png",@"调整顺序.png"];
                NSMutableArray *arr = [[NSMutableArray alloc]initWithCapacity:0];
                for (int i = 0; i < imgNameArr.count; i++) {
                    DYAdModel *adModel  = [[DYAdModel alloc]init];
                    adModel.imgStr      = imgNameArr[i];
                    adModel.linkUrl     = @"";
                    [arr addObject:adModel];
                }
                [self showGaussianBlur];
                [kWindow addSubview:[DYAdAlertView  showInView:kWindow theDelegate:self theADInfo:arr placeHolderImage:@"首页插入新栏目.png"]];
            }];
            [alert addAction:cameraAction];
            UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"查看[编辑]使用教程" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSArray * imgNameArr = @[@"工具栏介绍.png",@"词条使用.png",@"新增内容.png"];
                NSMutableArray *arr = [[NSMutableArray alloc]initWithCapacity:0];
                for (int i = 0; i < imgNameArr.count; i++) {
                    DYAdModel *adModel  = [[DYAdModel alloc]init];
                    adModel.imgStr      = imgNameArr[i];
                    adModel.linkUrl     = @"";
                    [arr addObject:adModel];
                }
                [self showGaussianBlur];
                [kWindow addSubview:[DYAdAlertView  showInView:kWindow theDelegate:self theADInfo:arr placeHolderImage:@"工具栏介绍.png"]];
            }];
            [alert addAction:libraryAction];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    
    if(indexPath.section == 1){
        if(indexPath.row == 0){
            PrivacyController * privacyController = [PrivacyController new];
            [self.navigationController pushViewController:privacyController animated:YES];
        }
        if(indexPath.row == 1){
            SuggestController * suggestController = [SuggestController new];
            [self.navigationController pushViewController:suggestController animated:YES];
        }
        if(indexPath.row == 2){
            AboutController * aboutController = [AboutController new];
            [self.navigationController pushViewController:aboutController animated:YES];
        }
        
    }
}

-(void)closeDown{
    [self hideGaussianBlur];
}
-(UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate =self;
        _tableView.dataSource=self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorColor = [UIColor colorWithAjenHex:0x7a7374 alpha:0.2];
        _tableView.backgroundColor = [UIColor colorWithAjenHex:0xffffff alpha:0.9];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, _tableView.bounds.size.width, CGFLOAT_MIN)];
        _tableView.tableHeaderView = tableViewHeaderView;
    }
    return _tableView;
}

@end
